package com.yutel.gui;

import com.yutel.biz.Call;
import com.yutel.biz.Login;
import com.yutel.main.WSClientMIDlet;
import com.yutel.util.LangUtil;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

/**
 *
 * @author yuanyajun
 */
public class IndexForm extends Form implements CommandListener, Runnable {

    private TextField prepPhone;
    private Command exitCom;
    private Command subCom;
    private Command chaCom;
    private Command setCom;
    private Command reCom;
    private Thread t;

    public IndexForm() {
        super(LangUtil.getInstance().getMessage(LangUtil.tit_index));
        try {
            exeute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void exeute() throws Exception {
        // Set up this Displayable to listen to command events
        prepPhone = new TextField(LangUtil.getInstance().getMessage(LangUtil.tag_prepPhone), "", 15, TextField.ANY);
        prepPhone.setConstraints(TextField.ANY);
        this.append(prepPhone);
        // add the Exit command
        exitCom=new Command(LangUtil.getInstance().getMessage(LangUtil.but_quit), Command.EXIT, 1);
        subCom=new Command(LangUtil.getInstance().getMessage(LangUtil.but_submit), Command.OK, 1);
        chaCom=new Command(LangUtil.getInstance().getMessage(LangUtil.but_charge), Command.OK, 1);
        setCom=new Command(LangUtil.getInstance().getMessage(LangUtil.but_option), Command.OK, 1);
        reCom=new Command(LangUtil.getInstance().getMessage(LangUtil.but_logout), Command.OK, 1);
        addCommand(exitCom);
        addCommand(subCom);
        addCommand(chaCom);
        addCommand(setCom);
        addCommand(reCom);
        setCommandListener(this);
    }

    public void commandAction(Command com, Displayable dis) {
        if (com == exitCom) {
            WSClientMIDlet.quitApp();
        } else if (com == subCom) {
            t = new Thread(this);
            t.start();
        } else if (com == setCom) {
            OptionForm option = new OptionForm();
            WSClientMIDlet.setDisplay(option.getList());
        } else if (com==chaCom) {
            ChargeForm charge = new ChargeForm();
            WSClientMIDlet.setDisplay(charge);
        } else if (com==reCom) {
            Login l=new Login();
            l.logout("aaaa");
            LoginForm login = new LoginForm();
            WSClientMIDlet.setDisplay(login);
        }
    }

    public void run() {
        Call call=new Call();
        String re=call.query(prepPhone.getString());
        StringItem resultItem = new StringItem("你的费率为:", "12.365");
        this.append(resultItem);
    }
}
